package lili.com.my.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.TextView;

import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import lili.com.my.R;

public class NewsActivity extends AppCompatActivity {


     String contentid;
     TextView titleTv;
     WebView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        textView= (WebView) findViewById(R.id.new_context);
        titleTv= (TextView) findViewById(R.id.new_tv);
        contentid = getIntent().getStringExtra("contentid");
        loading();
        loadingTitle();

    }

    public void loading() {
        RequestParams params = new RequestParams("http://web.csmzxy.com/netCourse/readData");
        params.addQueryStringParameter("cmd", "9");
        params.addQueryStringParameter("v1", contentid);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                String encoding="UTF-8";
                String mimeType="text/html";
                textView.loadDataWithBaseURL("file://",result,mimeType,encoding,"about:blank");

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
    public void loadingTitle() {
        RequestParams params = new RequestParams("http://web.csmzxy.com/netCourse/readData");
        params.addQueryStringParameter("cmd", "41");
        params.addQueryStringParameter("v1", contentid);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson=new Gson();
              NewTitleModel newTitleModel=  gson.fromJson(convertString(result),NewTitleModel.class);
                titleTv.setText(newTitleModel.contentTitle);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
    public String convertString(String result) {
        String result1 = result
                .replace(", responseSign : 00", "")
                .replace("\'", "\"")
                .replace("contentID", "\"contentID\"")
                .replace("contentAuthor", "\"contentAuthor\"")
                .replace("contentTitle", "\"contentTitle\"")
                .replace("submitTime", "\"submitTime\"");
        return result1;

    }

}
